package edu.hawaii.examschd.modules.CouchDB;

import static org.junit.Assert.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.junit.Test;

public class TestCouchDBClient {

  CouchDBClient testClient;
  String host = "127.0.0.1";
  int port = 5984;
  String req = "/uhm/_design/uhm/_view/crn?key=\"81455\"";
  String error;
  String line;
  String header;

  public TestCouchDBClient() {
    testClient = new CouchDBClient(host, port);
    line = testClient.send(req);
    header = testClient.construct_http_get(req);
  }

  @Test
  public void testConnect() {
    error = "testConnect failed: Return value does not contain expected values";
    line = testClient.send(req);
    assertTrue(error, line.contains("81455"));
    assertTrue(error, line.contains("ICS"));
    assertTrue(error, line.contains("1730"));
  }

  @Test
  public void testConstructHttpGet() {
    error = "testConstructHttpGet failed: malformed header created.";
    assertTrue(error, header.contains("GET"));
    assertTrue(error, header.contains(req));
    assertTrue(error, header.contains(host));
    assertTrue(error, header.contains("text/html"));
  }

  @Test
  public void testCheckPort() {
    error = "testCheckPort failed: accepts invalid ports";
    assertTrue(error, testClient.checkPort(5984) == 5984);
    assertFalse(error, testClient.checkPort(77777) == 77777);
    assertTrue(error, testClient.checkPort(8080) == 8080);
    assertFalse(error, testClient.checkPort(80) == 80);

  }

  @Test
  public void testConstruct_cal() {
    DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
    error = "testConstruct_cal failed: returns invlaid value";
    assertTrue(
        error,
        dateFormat.format(testClient.construct_exam_cal("2013,5,10,0730,0930").getTime()).contains(
            "20130610T0730"));
  }
}